
// Extra/pages/HealthPlan/index.js
import {
    fetchPlanList,
    fetchUpdatePlan,
    fetchInsertPlan,
    fetchDeletePlan
  } from '@/services/plan';
  import { fetchDeviceInfo } from '@/services/device';
  import dayjs from 'dayjs';
  import { showToast, showModal, getParams } from '@/utils/util';
  
  const app = getApp();
  
  Page({
    data: {
      staticPrefix: app.globalData.STATIC_PREFIX,
      barHeight: app.globalData.IPHONE_BAR_HEIGHT,
      formData: {},
      list: []
    },
  
    async onLoad(options) {
      const { device_imei, watch_info_id } = getParams(options.params);
      const res = await fetchDeviceInfo({ device_imei });
      if (res.return_code === 'SUCCESS') {
        this.setData(
          {
            device_info: res.data,
            device_imei,
            watch_info_id,
            defaultDate: dayjs(new Date()).format('YYYY-MM-DD')
          },
          this.searchList
        );
      }
    },
  
    async searchList() {
      const { defaultDate, watch_info_id } = this.data;
      const res = await fetchPlanList({
        stime: defaultDate,
        watch_info_id,
        page_size: 9999,
        page_num: 1
      });
      if (res.return_code === 'SUCCESS') {
        this.setData({ list: res.list });
      }
    },
  
    onDateSelect(event) {
      this.setData(
        { defaultDate: dayjs(event.detail).format('YYYY-MM-DD') },
        this.searchList
      );
    },
  
    onPlanAdd() {
      this.setData({
        show: true,
        formData: {
          device_name: this.data.device_info.device_name,
          remind_date: this.data.defaultDate,
          watch_info_id: this.data.watch_info_id,
          device_imei: this.data.device_info.device_imei
        }
      });
    },
  
    onPlanEdit(event) {
      const { item } = event.currentTarget.dataset;
      const weekMap = {
        '星期天': '周日',
        '星期一': '周一',
        '星期二': '周二',
        '星期三': '周三',
        '星期四': '周四',
        '星期五': '周五',
        '星期六': '周六'
      };
  
      this.setData({
        show: true,
        formData: {
          days: item.remind_cycle.split(',').map((day) => weekMap[day]),
          id: item.id,
          remind_type: item.remind_type,
          remind_model: item.remind_model,
          remind_time: item.remind_model ? item.remind_time.split(' ')[1] : item.remind_time,
          device_name: this.data.device_info.device_name,
          remind_date: this.data.defaultDate,
          watch_info_id: this.data.watch_info_id,
          device_imei: this.data.device_imei
        }
      });
    },
  
    onPlanDelete(e) {
      showModal('是否确认删除该条健康计划？', async () => {
        const res = await fetchDeletePlan({ id: e.currentTarget.dataset.item.id });
        if (res.return_code === 'SUCCESS') {
          showToast('健康计划已删除', this.searchList);
        }
      });
    },
  
    async onFormConfirm(e) {
      const { detail } = e;
      const res = detail.id
        ? await fetchUpdatePlan(detail)
        : await fetchInsertPlan(detail);
      if (res.return_code === 'SUCCESS') {
        showToast(
          detail.id ? '编辑计划成功' : '新建计划成功',
          () => {
            this.searchList();
            this.setData({ show: false, formData: {} });
          }
        );
      }
    },
  
    onFormClose() {
      this.setData({ show: false, formData: {} });
    }
  });
  